//
//  UIView+Extension.swift
//  HHSwift
//
//  Created by 华宏 on 2019/5/14.
//  Copyright © 2019年 huahong. All rights reserved.
//

import Foundation
import QuartzCore

extension UIView{
    
    /// origin
    var origin: CGPoint {
        get {
            return self.frame.origin
        }
        set {
            var frame = self.frame
            frame.origin = newValue
            self.frame = frame
        }
    }
    
    
    /// size
    var size: CGSize {
        get {
            return self.frame.size
        }
        set {
             var frame = self.frame
            frame.size = newValue
            self.frame = frame
        }
    }
    
    ///width
    var width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
    }
    
    ///height
    var height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
    }
    
    ///top
    var top: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    
    ///bottom
    var bottom: CGFloat {
        get {
            return top + height
        }
        
        set{
            var frame = self.frame
            frame.origin.y = newValue - frame.size.height
            self.frame = frame
            
        }
    }
    
    ///left
    var left: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
    }
    
    ///right
    var right: CGFloat {
        get {
            return left + width
        }
        set{
            var frame = self.frame
            frame.origin.x = newValue - frame.size.width
            self.frame = frame
            
        }
    }
    
    ///center
    var center: CGPoint {
        get {
            return CGPoint(x: left + width/2.0, y: top + height/2.0)
        }
        set {
            var frame = self.frame
            frame.origin.x = newValue.x - width/2.0
            frame.origin.y = newValue.y - height/2.0
            self.frame = frame
        }
    }
    
    var viewController: UIViewController? {
        var nextResponder = self.next
        
            repeat{
                if nextResponder?.isKind(of: UIViewController.classForCoder()) == true {
                    return next as? UIViewController
                }
               nextResponder = nextResponder?.next
                
            }while (nextResponder != nil)
        
        
       return nil
    }
    
    
    /// 截屏
    func snapshotImage() -> UIImage? {
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0)
        self.layer.render(in: context)
        let snap = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snap
        
    }
    
    
    /// 屏幕更新后截屏
    ///
    /// - Parameter afterUpdates: 是否更新
    /// - Returns: UIImage
    func snapshotImageAfterScreenUpdates(afterUpdates:Bool) -> UIImage? {
        if self.responds(to: #selector(drawHierarchy(in:afterScreenUpdates:))) {
            
            UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0)
            self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
            let snap = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return snap
        }else
        {
          return self.snapshotImage()
        }
    }
    
//    - (NSData *)snapshotPDF {
//    CGRect bounds = self.bounds;
//    NSMutableData* data = [NSMutableData data];
//    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)data);
//    CGContextRef context = CGPDFContextCreate(consumer, &bounds, NULL);
//    CGDataConsumerRelease(consumer);
//    if (!context) return nil;
//    CGPDFContextBeginPage(context, NULL);
//    CGContextTranslateCTM(context, 0, bounds.size.height);
//    CGContextScaleCTM(context, 1.0, -1.0);
//    [self.layer renderInContext:context];
//    CGPDFContextEndPage(context);
//    CGPDFContextClose(context);
//    CGContextRelease(context);
//    return data;
//    }
//    func snapshotPDF() -> Data? {
//
//        var data = Data()
//        let consumer = CGDataConsumer(data: data as! CFMutableData)
//        let context = kCGPDFContextCreator(consumer)
//
//
//    }
  
    func setLayerShadow(color:UIColor,offset:CGSize,radius:CGFloat) {
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = offset
        self.layer.shadowRadius = radius
        self.layer.shadowOpacity = 1.0
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.main.scale
    }
    
    
    
    /// 移除所有子视图
    func removeAllSubviews() {
        while self.subviews.count > 0 {
          self.subviews.last?.removeFromSuperview()
        }
    }
    
   
    
    //    override func draw(_ rect: CGRect) {
    //
    //        guard let context = UIGraphicsGetCurrentContext() else {
    //            print("Error")
    //            return
    //        }
    //
    //        context.setStrokeColor(UIColor.red.cgColor)
    //        let strokeWidth: CGFloat = 3.0
    //        context.setLineWidth(3.0)
    //        let center = CGPoint(x: self.frame.midX, y: self.frame.midY)
    //        let radius = self.frame.midX - strokeWidth
    //        let startAngle: CGFloat = 0.0
    //        let endAngle = 2 * CGFloat.pi
    //
    //        context.addArc(center: center,
    //                       radius: radius,
    //                       startAngle: startAngle,
    //                       endAngle: endAngle,
    //                       clockwise: false)
    //
    //        context.drawPath(using: .stroke)
    //    }
    
    
    
    /*自动布局
     flexibleLeftMargin  自动调整view与父视图左边距，保证右边距不变
     flexibleWidth       自动调整view的宽度，保证左边距和右边距不变
     flexibleRightMargin 自动调整view与父视图右边距，以保证左边距不变
     flexibleTopMargin   自动调整view与父视图上边距，以保证下边距不变
     flexibleHeight      自动调整view的高度，以保证上边距和下边距不变
     flexibleBottomMargin自动调整view与父视图的下边距，以保证上边距不变
     */
//    self.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    
}
